function AtMon(hObject, eventdata, handles)
function [readval] = SerialReadVal()
	char_minimum = ftell(f1)+4;
	watchdog=0;
	dataexist=1;
	while (handles.hrealterm.charcount<char_minimum)
		pause(1E-6);
		inc watchdog;
		if(watchdog>50)
			dataexist=0;
			break;
		end
	end
	
	%keyboard;
	if (dataexist)
			v1 = cast(fscanf(f1,'%c',1),'uint8');
			v2 = cast(fscanf(f1,'%c',1),'uint8');	
			v3 = cast(fscanf(f1,'%c',1),'uint8');
			v4 = cast(fscanf(f1,'%c',1),'uint8');
			vstr = strcat(v2,v3,v4);
			[vval, stat] = str2num(vstr);
			if(stat)
				if (isempty(vval))
					readval = 0;
				else
					readval = vval;
				end
			else
				readval = 0;				
			end		
	else
		readval = -1;	
	end	
end
%FILTER
filteron = 1;
if filteron 
	H = fir1(7, 0.105, 'low');
	rtfilter([1 1 1],H);
end;

%update tiap xdivisor sample
xdivisor = 4;
imbuffer = 'sbuf';
handles.hrealterm.CaptureFile=strcat(cd,'\',imbuffer);
invoke(handles.hrealterm,'startcapture'); 	

f1 = fopen(imbuffer);

axes(handles.acc);
dataX = handles.accX;
dataY = handles.accY;
dataZ = handles.accZ;
set(handles.HX,'YData',dataX);
set(handles.HY,'YData',dataY);
set(handles.HZ,'YData',dataZ);
drawnow

	handles.hrealterm.PutString('aaa');
strtemp=sprintf('Waiting for data..');
set(handles.debug,'string',enquestr(strtemp));

while(handles.hrealterm.charcount==0)
	pause(1E-6);
end

x=toc;
strtemp=sprintf('Data received at %3.2f seconds',x);
set(handles.debug,'string',enquestr(strtemp));

char_minimum=0;
cur_sample=0;
dataexist=1;
data = zeros(1,3);
while(1)
	header=SerialReadVal();
	while((header~=888)&&(header~=-1))
		%fprintf('h %d\n',header);
		header=SerialReadVal();
	end
	if(header==-1)
		break;
	end;
	inc cur_sample;
	if (cur_sample>handles.ncell)
		cur_sample=1;
		dataX = handles.accX;
		dataY = handles.accY;
		dataZ = handles.accZ;
	end
	rawdata(1) = SerialReadVal();
	rawdata(2) = SerialReadVal();
	rawdata(3) = SerialReadVal();
	
	%fprintf('#%d - %d - %d \n',rawdata);
	if filteron
		rawdata(1:3)= rtfilter(rawdata(1:3));
	end;
	
	dataX(cur_sample)=rawdata(1);
	dataY(cur_sample)=rawdata(2);
	dataZ(cur_sample)=rawdata(3);
	
	%if(mod(cur_sample,xdivisor)==0)
		%Acc
		set(handles.HX,'YData',dataX);
		set(handles.HY,'YData',dataY);
		set(handles.HZ,'YData',dataZ);
		drawnow;
	%end;
	
end

fclose(f1);
invoke(handles.hrealterm,'stopcapture');

%handles.accX = dataX;
%handles.accY = dataY;
%handles.accZ = dataZ;
a=fix(clock);
%fname = sprintf('data%02d%02d_%02d%02d.log',a(3),a(2),a(4),a(5));
%copyfile(imbuffer,fname);
%movefile(fname,'log\');
%keyboard;
[filename, pathname] =  uiputfile({'*.xls'},'Save log as');
fname = [pathname filename];	
data = dlmread(imbuffer);
xlswrite(fname,data(:,2:4));
 
strtemp=sprintf('Data saved at %s',fname);
set(handles.debug,'string',enquestr(strtemp));
guidata(hObject, handles);
end
